package com.doyens.item.service;

import com.doyens.item.mapper.SpecGroupMapper;
import com.doyens.item.mapper.SpecParamMapper;
import com.doyens.item.pojo.SpecGroup;
import com.doyens.item.pojo.SpecParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.peer.ScrollbarPeer;
import java.util.List;

@Service
public class SpecificationService {

    private static final Logger  LOGGER = LoggerFactory.getLogger(SpecificationService.class);


    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper  specParamMapper;

    public List<SpecGroup>  queryGroupByCid(Long cid){
        LOGGER.info("查询规格组信息");
        SpecGroup group  = new SpecGroup();
        group.setCid(cid);
        return this.specGroupMapper.select(group);
    }

    public List<SpecParam> queryParams(Long gid, Long cid, Boolean  generic, Boolean searching) {
        LOGGER.info("查询规格组下的规格参数信息");  // warn   error  debug  info
        SpecParam  param = new SpecParam();
        param.setGroupId(gid);
        param.setCid(cid);
        param.setGeneric(generic);
        param.setSearching(searching);
        List<SpecParam> specParamList = specParamMapper.select(param);
        return specParamList;
    }

    //根据分类id查询规格组合规格参数
    public List<SpecGroup> queryGroupWithParamByCid(Long cid) {
        List<SpecGroup> groups = this.queryGroupByCid(cid);
        groups.forEach(group ->{
            List<SpecParam> params = this.queryParams(group.getId(), null, null, null);
            group.setParams(params);
        });
        return groups;
    }
}
